package com.kuang.mapper;


import com.kuang.springcloud.pojo.User;
import feign.Param;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserMapper {
    @Select("select * from user where username=#{username}")
    User getUserByName(String username);

    @Select("select * from user")
    List<User> getUsers();

    @Insert("insert into user(username,password,role) value(#{username},#{password},#{role})")
    int addUser(User user);

    @Select("select * from user where username=#{username}")
    User findByUsername(Integer username);

    @Select("select * from user where id=#{id}")
    User findUserById(Integer id);

    @Select("select username from user where id=#{id}")
    String  findUsernameById(Integer id);

    //根据学号找到用户的注册时候的预设问题
    @Select("select question from user where username=#{username}")
    User findQuestionByusername(Integer  username);

    //根据id找到用户的注册时候的预设答案
    @Select("select answer from user where username=#{username}")
    User findAnswerByUsername(int username);

    //根据id找到用户的注册时候的密码
    @Select("select password from user where user_id=#{user_id}")
    User findPasswordById(int id);

    //根据xuehao 找到用户的注册时候的密码
    @Update("update user set password=#{password} where username=#{username}")
    Integer updatePassword(User user);

    //根据username找到用户的id
    @Update("select user_id from user where username=#{username}")
    int findidByUsername(User user);

    //注册信息
    @Insert("insert into user (username,password,real_name,role,mobile,access_token,question,answer,academy,major,team_id,sex) " +
            "values (#{username},#{password},#{real_name},#{role},#{mobile},#{access_token},#{question},#{answer},#{academy},#{major},#{team_id},#{sex})")
     int registUser(User user);

    //上传头像
    @Insert("UPDATE user set avatar=#{avatar} where user_id=#{user_id}")
    int uoloadAvatar(User user);

    //根据user_id找到username
    @Select("select username from user where user_id=#{user_id}")
    String getUsernameByUser_id(Integer user_id);

    @Update("update user set  real_name=#{real_name},mobile=#{mobile},academy=#{academy}," +
            " major=#{major}, access_token=#{access_token},sex=#{sex} where user_id=#{user_id}")
    int updateUser(User user);

    //根据user_id传基本信息的头像
    @Select("select avatar from user where user_id=#{uid}")
    String findAvatarById(Integer uid);


    //根据用户id寻找用户名
    @Select("select real_name from user where user_id=#{user_id}")
    String findRealnameById(Integer id);


    //判断头像是否为空
    @Select("select exists(select 1 from user where user_id=#{uid} and avatar is not null)")
    Boolean isAvatarExist(Integer uid);

    @Update("update user set avatar=#{avatar} where user_id=#{uid}")
    int updateUserAvatar(@Param("uid") Integer uid, @Param("avatar") String avatar);


    //判断uid是否存在
    @Select("select exists(select 1 from user where user_id =#{uid})")
    Boolean isUIDExist(Integer uid);

    @Select("select * from user where user_id=#{uid}")
    User findUseByUID(Integer uid);

    @Select("select username  from user ")
    List<Integer> findUseanmeByUID();
}